package com.lys.controller;

import com.lys.entity.Product;
import com.lys.service.ProductCategoryService;
import com.lys.service.ProductService;
import com.lys.utils.EmptyUtils;
import com.lys.utils.Pager;
import com.lys.utils.ProductCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/30 7:51
 */
@Controller
@RequestMapping("/product")
public class ProductController {


    private ProductService productService;
    private ProductCategoryService productCategoryService;

    @Autowired
    public ProductController(ProductService productService, ProductCategoryService productCategoryService) {
        this.productService = productService;
        this.productCategoryService = productCategoryService;
    }


    /**
     * 查询商品列表
     * @return
     */
    @GetMapping
    public String queryProductList(@RequestParam(value = "category",required = false) String category,
                                   @RequestParam(value = "level",required = false) String levelStr,
                                   @RequestParam(value = "currentPage",required = false) String currentPageStr,
                                   @RequestParam(value = "keyWord",required = false) String keyWord,
                                   @RequestParam(value = "pageSize",required = false) String pageSizeStr,
                                   Model model) {
        //String category = request.getParameter("category");
        //String levelStr = request.getParameter("level");
        //String currentPageStr = request.getParameter("currentPage");
        //String keyWord = request.getParameter("keyWord");
        //获取页大小
        //String pageSizeStr = request.getParameter("pageSize");
        int rowPerPage = EmptyUtils.isEmpty(pageSizeStr) ? 20:Integer.parseInt(pageSizeStr); // 每页显示多少个
        int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr); // 当前页面
        int  level=EmptyUtils.isNotEmpty(levelStr)?Integer.parseInt(levelStr):0;
        int total = productService.count(keyWord, EmptyUtils.isEmpty(category)?null:Integer.valueOf(category), level);
        Pager pager = new Pager(total, rowPerPage, currentPage);
        pager.setUrl("product?level="+level+"&category="+(EmptyUtils.isEmpty(category)?"":category));
        List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategoryList();
        List<Product> productList = productService.getProductList(currentPage, rowPerPage, keyWord, EmptyUtils.isEmpty(category)?null:Integer.valueOf(category), level);
        model.addAttribute("productList", productList);
        model.addAttribute("pager", pager);
        model.addAttribute("total", total);
        model.addAttribute("keyWord", keyWord);
        model.addAttribute("productCategoryVoList", productCategoryVoList);
        return "/pre/product/queryProductList";
    }
    /**
     *
     * @param request
     * @return
     */
    @GetMapping("/{id}")
    public String queryProductDeatil(HttpServletRequest request,
                                     @PathVariable("id") String id) {
        //String id = request.getParameter("id");
        Product product = productService.getProductById(Integer.valueOf(id));
        List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategoryList();
        request.setAttribute("product", product);
        request.setAttribute("productCategoryVoList", productCategoryVoList);
        try {
            addRecentProduct(request,product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/pre/product/productDeatil";
    }
    /**
     * 查询最近商品
     * @return
     */
    private List<Product> queryRecentProducts(HttpSession session){
        // HttpSession session=request.getSession();
        List<Product> recentProducts= (List<Product>) session.getAttribute("recentProducts");
        if(EmptyUtils.isEmpty(recentProducts)){
            recentProducts=new ArrayList<Product>();
        }
        return recentProducts;
    }
    /**
     * 添加最近浏览商品
     * @param request
     * @param product
     */
    private void addRecentProduct(HttpServletRequest request,Product product) {
        HttpSession session=request.getSession();
        List<Product> recentProducts=queryRecentProducts(session);
        //判断是否满了
        if(recentProducts.size()>0 &&  recentProducts.size()==10){
            recentProducts.remove(0);
        }
        recentProducts.add(recentProducts.size(),product);
        session.setAttribute("recentProducts",recentProducts);
    }
}
